package com.example.admin.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@WebFilter("/*")
@Slf4j
@Component
public class AAStaticFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String requestURI = request.getRequestURI();
        log.info("静态资源Filter拦截了");

//        //避免后续的拦截器处理flag的时候抛出空指针异常
//        request.setAttribute("flag",true);
//        if (requestURI.endsWith("html") || requestURI.endsWith("js") || requestURI.endsWith("css") || "/blog/findAll".equals(requestURI)){
//            //放置信号量 setAttribute-往请求中增加数据
//            request.setAttribute("flag",true);
//        }
        //比较老练的方案-推荐
        request.setAttribute("flag",requestURI.endsWith("html") || requestURI.endsWith("js") || requestURI.endsWith("css"));

        //放行
        filterChain.doFilter(servletRequest,servletResponse);
        log.info("又回到静态资源Filter");
    }
}
